﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;

public partial class Probe_1CReturns : System.Web.UI.Page
{

    #region Members

    DataTable tblMain = new DataTable();
    DataTable tblKontragent = new DataTable();
    DataTable tblSklad = new DataTable();

    DateTime dtNow = DateTime.Now;
    DateTime dtFrom = DateTime.Now;
    DateTime dtTo = DateTime.Now;

    bool bDateCtrlLock = false;

    #endregion

    #region Page

    protected void Page_PreInit(object sender, EventArgs e)
    {
        //Page.MasterPageFile = "~/MyMasterPage.master";
        dtNow = new DateTime(dtNow.Year, dtNow.Month, dtNow.Day);
        dtFrom = dtNow;
        dtTo = dtNow;
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        DateTime dtNow = DateTime.Now;

        if (!IsPostBack)
        {
            oDTFrom.SelectedDate = dtFrom;
            oDTFrom.VisibleDate = dtFrom;
            oDTTo.SelectedDate = dtTo;
            oDTTo.VisibleDate = dtTo;
            Exception err = new Ko2.DB.OneC().GetReturnsKontragent(ref tblKontragent);
            if (err == null)
            {
                listKontragent.DataSource = tblKontragent;
                listKontragent.DataValueField = "Kontragent";
                listKontragent.DataBind();
            }
            err = new Ko2.DB.OneC().GetReturnsSklad(ref tblSklad);
            if (err == null)
            {
                listSklad.DataSource = tblSklad;
                listSklad.DataValueField = "Sklad";
                listSklad.DataBind();
            }
        }
        else
        {
            dtFrom = oDTFrom.SelectedDate;
            dtTo = oDTTo.SelectedDate;
            if (dtFrom > dtTo)
            {
                dtFrom = dtTo;
                oDTFrom.SelectedDate = dtFrom;
            }

        }
    }

    #endregion

    #region Ctrl Events

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        tblMain = new DataTable();
        Exception err = Get();
        if (err == null)
        {
            gv.DataBind();
            lblInfo.Visible = false;
        }
        else
        {
        }
    }

    protected void oDTFrom_SelectionChanged(object sender, EventArgs e)
    {
        if (bDateCtrlLock)
            return;
        bDateCtrlLock = true;
        tblMain = new DataTable();
        gv.DataBind();
        lblInfo.Visible = true;
        dtFrom = oDTFrom.SelectedDate;
        dtTo = oDTTo.SelectedDate;
        if (dtFrom > dtTo)
        {
            dtFrom = dtTo;
            oDTFrom.SelectedDate = dtFrom;
        }
        bDateCtrlLock = false;
    }

    protected void oDTTo_SelectionChanged(object sender, EventArgs e)
    {
        if (bDateCtrlLock)
            return;
        bDateCtrlLock = true;
        tblMain = new DataTable();
        gv.DataBind();
        lblInfo.Visible = true;
        dtFrom = oDTFrom.SelectedDate;
        dtTo = oDTTo.SelectedDate;
        if (dtFrom > dtTo)
        {
            dtFrom = dtTo;
            oDTFrom.SelectedDate = dtFrom;
        }
        bDateCtrlLock = false;
    }

    protected void checkKontragent_CheckedChanged(object sender, EventArgs e)
    {
        tblMain = new DataTable();
        gv.DataBind();
        lblInfo.Visible = true;
    }

    protected void checkSklad_CheckedChanged(object sender, EventArgs e)
    {
        tblMain = new DataTable();
        gv.DataBind();
        lblInfo.Visible = true;
    }

    protected void listKontragent_SelectedIndexChanged(object sender, EventArgs e)
    {
        tblMain = new DataTable();
        gv.DataBind();
        lblInfo.Visible = true;
    }

    protected void listSklad_SelectedIndexChanged(object sender, EventArgs e)
    {
        tblMain = new DataTable();
        gv.DataBind();
        lblInfo.Visible = true;
    }

    #endregion

    #region Data

    private Exception Get()
    {
        Exception ret = null;
        string Kontragent = "";
        string Sklad = "";

        if (checkKontragent.Checked)
            Kontragent = listKontragent.SelectedValue;
        if (checkSklad.Checked)
            Sklad = listSklad.SelectedValue;

        Exception err = new Ko2.DB.OneC().GetReturns(ref tblMain, dtFrom, dtTo, Kontragent, Sklad);
        if (err == null)
        {
            tblMain.Columns[0].ColumnName = "Дата";
            tblMain.Columns[0].Caption = "Дата";
            tblMain.Columns[1].ColumnName = "Склад";
            tblMain.Columns[1].Caption = "Склад";
            tblMain.Columns[2].ColumnName = "Контрагент";
            tblMain.Columns[2].Caption = "Контрагент";
            tblMain.Columns[3].ColumnName = "Номер";
            tblMain.Columns[3].Caption = "Номер";
            tblMain.Columns[4].ColumnName = "Сумма";
            tblMain.Columns[4].Caption = "Сумма";
            gv.DataSource = tblMain;
        }
        return ret;
    }

    protected void oDTFrom_DataBinding(object sender, EventArgs e)
    {
    }

    protected void oDTTo_DataBinding(object sender, EventArgs e)
    {
    }

    protected void gv_DataBinding(object sender, EventArgs e)
    {
    }

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Font.Bold = true;
            e.Row.Cells[0].Text = e.Row.Cells[0].Text.Substring(0, 10);
            Decimal value = Convert.ToDecimal(e.Row.Cells[4].Text);
            e.Row.Cells[4].Text = value.ToString("C");
            e.Row.Cells[4].Font.Bold = true;
            e.Row.Cells[4].ForeColor = Color.Gray;
        }
        if (e.Row.RowIndex == -1)
        {
        }
    }

    #endregion

}